/**
 * Created by wangxiaoqiang on 2017/9/18.
 */
let app = new Vue({
    el: "#add_good",
    data: {
        resources: {
            categoryList: [],
            freightTempList: [],
            attrClassList: [],
            attrList: [],
            mainImg:null,
            bannerImg:null,
            imgPath: fileServerPath,
        }
    },
    mounted: function () {
        this.getCategoryList();
        this.getFreightTempList();
        this.getAttrClassList();
        this.inpValidate();
    },
    methods: {
        //获取分类列表
        getCategoryList: function () {
            $.get(rootPath + "/category/all", (res) => {
                if (res.meta.code === 200) {
                    this.resources.categoryList = res.data;
                } else console.log(res.meta.code)
            })
        },
        //获取运费模板列表
        getFreightTempList: function () {
            $.get(rootPath + "/freight/all", (res) => {
                if (res.meta.code === 200) {
                    this.resources.freightTempList = res.data;
                } else console.log(res.meta.code)
            })
        },
        //获取规格属性类别列表
        getAttrClassList: function () {
            $.get(rootPath + "/goods/attr/class/all", (res) => {
                if (res.meta.code === 200) {
                    this.resources.attrClassList = res.data;
                } else console.log(res.meta.code)
            })
        },
        //上传规格图片，添加规格到临时列表
        addAttr: function () {
            let attr_data = new NiceValidate([
                {dom: document.getElementById("attr_class")},
                {dom: document.getElementById("attr_name")},
                {dom: document.getElementById("increase_price")},
                {dom: document.getElementById("attr_desc"),isNull:true},
                {dom: document.getElementById("attr_number"),isNull:true},
            ]).niceValidate();

            if (attr_data) {
                //设置按钮不可点击
                if(!attr_data.attr_number){
                    attr_data.attr_number = 0
                }
                setStatus.setDisable($("#attr_submit"));
                let files = document.querySelector("#attr_img").files;

                //上传数据
                uploadFile(files).then((res) => {
                    if(res.meta.code === 200){
                        attr_data.attr_img = res.data.length ? res.data[0].savePath : "";
                        for(let item in this.resources.attrClassList){
                            if(attr_data.attr_class == this.resources.attrClassList[item].id){
                                attr_data.attr_class_object = this.resources.attrClassList[item]
                            }
                        }
                        this.resources.attrList.push(attr_data);
                        setStatus.setEnable($("#attr_submit"), '<i class="fa fa-plus"></i>');//恢复按钮点击
                    }else alert("图片上传失败--"+res.meta.code)
                })
            }
        },

        //删除item
        deleteAttrItem: function (e) {
            let item_index = e.target.getAttribute("item_index");
            switch (e.target.getAttribute("img_type")) {
                case "main_img":
                    this.resources.mainImg.forEach((item, index) => {
                        if (index == item_index) {
                            this.resources.mainImg.splice(parseInt(item_index), 1);
                        }
                    });
                    break;
                case "banner_img":
                    this.resources.bannerImg.forEach((item, index) => {
                        if (index == item_index) {
                            this.resources.bannerImg.splice(parseInt(item_index), 1);
                        }
                    });
                    break;
                case "attr_img":
                    this.resources.attrList.forEach((item, index) => {
                        if (index == item_index) {
                            this.resources.attrList.splice(parseInt(item_index), 1);
                        }
                    });
                    break;
                default:
                    break
            }
        },

        //上传商品的展示主图
        uploadMainImg:function () {
            let files = document.querySelector("#pic_main").files;
            //上传数据
            uploadFile(files).then((res) => {
                console.log(res)
                if(res.meta.code === 200){
                    this.resources.mainImg = res.data;
                }else alert("图片上传失败--"+res.meta.code);
            })
        },

        //上传商品banner图
        uploadBannerImg:function () {
            let files = document.querySelector("#pic_banner").files;
            //上传数据
            uploadFile(files).then((res) => {
                console.log(res)
                if(res.meta.code === 200){
                   this.resources.bannerImg = res.data;
                }else alert("图片上传失败--"+res.meta.message)
            })
        },

        //输入验证
        inpValidate:function () {
            return new NiceValidate([
                {dom: document.getElementById("goods_name"),range:[1,25]},
                {dom: document.getElementById("cat_id")},
                {dom: document.getElementById("goods_sn")},
                {dom: document.getElementById("goods_number"),type:"number"},
           /*     {dom: document.getElementById("goods_weight"),},*/
                {dom: document.getElementById("price")},
                {dom: document.getElementById("member_price")},
                {dom: document.getElementById("goods_brief")},
            /*    {dom: document.getElementById("goods_desc")},*/
                {dom: document.getElementById("seller_note"),isNull:true},
            /*    {dom: document.getElementById("freight_id")},*/
            ])
        },

        //上传商品所有信息
        uploadGoods:function () {
            let goods_data = this.inpValidate().niceValidate();
            if(goods_data){
                setStatus.setDisable($("#submit"));
                goods_data.goodsAttrs_json = JSON.stringify(this.resources.attrList);   //商品规格
                goods_data.pic_main = JSON.stringify(this.resources.mainImg);           //主图
                goods_data.pic_banner = JSON.stringify(this.resources.bannerImg);       //商品banner图
                goods_data.freight_id = 1; //默认1
                $.post(rootPath+"/goods",goods_data,function (res) {
                    if(res.meta.code === 200){
                        setStatus.setEnable($("#submit"),"提交");
                        alert("添加成功")
                    }else alert("添加失败--"+res.meta.message)
                });
            }
        }
    }
});



